package com.sat.goods.controller;

import com.sat.goods.pojo.Brand;
import com.sat.goods.service.BrandService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result<>(true, StatusCode.OK,"查询成功",brandList);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK,"查询成功",brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result<>(true, StatusCode.OK,"添加记录");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand){
        brandService.update(brand);
        return new Result<>(true, StatusCode.OK,"修改记录");
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        brandService.delete(id);
        return new Result<>(true, StatusCode.OK,"删除成功");
    }

    @PostMapping("/search")
    public Result searchByBrand(@RequestBody Brand brand){
        List<Brand> brandList = brandService.searchByBrand(brand);
        return new Result<>(true, StatusCode.OK,"搜索成功",brandList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result page(@PathVariable Integer page,@PathVariable Integer size){

        PageResult<Brand> pageResult = brandService.findPage(page,size);
        return new Result<>(true, StatusCode.OK,"搜索成功",pageResult);
    }

    /**
     * 根据条件分页查询
     * @param page
     * @param size
     * @param brand
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable Integer page,@PathVariable Integer size,@RequestBody Brand brand){
        PageResult<Brand> pageResult = brandService.findPage(page,size,brand);
        return new Result<>(true, StatusCode.OK,"搜索成功",pageResult);
    }

    /**
     * 根据分类id查找对应分类的品牌
     * @param categoryId
     * @return
     */
    @GetMapping(value = "/category/{id}")
    public Result<List<Brand>> findBrandByCategoryId(@PathVariable(value = "id")Integer categoryId){
        //调用Service查询品牌数据
        List<Brand> categoryList = brandService.findBrandByCategoryId(categoryId);
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功！",categoryList);
    }
}
